package co.yixiang.modules.shop.rest.Arest;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.*;
import cn.hutool.crypto.SecureUtil;
import co.yixiang.modules.shop.domain.*;
import co.yixiang.modules.shop.service.*;
import co.yixiang.modules.shop.service.dto.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.*;

@Api(tags = "Api2:商城:购物车")
@RestController
@RequestMapping("api2/yxStoreOrder")

public class AStoreOrderController extends ABaseController {

    @Autowired
    public YxStoreOrderService yxStoreOrderService;
    @Autowired
    public YxStoreCartService yxStoreCartService;
    @Autowired
    public YxStoreProductService yxStoreProductService;
    @Autowired
    public YxStoreOrderCartInfoService orderCartInfoService;

    //sku表
    @Autowired
    public YxStoreProductAttrValueService storeProductAttrValueService;

    @Autowired
    private YxUserAddressService userAddressService;


    @ApiOperation(value = "结算",notes = "商品页的立刻购买时传orderItem，购物车的结算时传购物车id列表cartItemIds")
    @PostMapping("/confirm")
    public PreOrderDto confirm(@Validated @RequestBody OrderParam orderParam){
        PreOrderDto preOrderDto = new PreOrderDto();//返回的对象
        //设置uid
        preOrderDto.setUid(orderParam.getUid());

        List<Long> cartItemIds = orderParam.getCartItemIds();
        YxStoreCart orderItem = orderParam.getOrderItem();
        List<YxStoreCart> carts = null;
        //购物车结算
        if(cartItemIds!=null && !cartItemIds.isEmpty()){
            carts = new ArrayList<>(yxStoreCartService.listByIds(cartItemIds));
        }
        else if(orderItem !=null){
            //立刻购买
            carts = new ArrayList<>();
            carts.add(orderItem);
        }

        //设置商品列表信息
        List<OrderShopItemDto> orderShopItemDtos = yxStoreOrderService.convertCartItems(carts);
        preOrderDto.setOrderShopItemDtos(orderShopItemDtos);

        //设置其他总价格、数量
        for (OrderShopItemDto o : orderShopItemDtos) {
            preOrderDto.setTotalPrice(preOrderDto.getTotalPrice().add(o.getTotalPrice()));
            preOrderDto.setTotalPostage(preOrderDto.getTotalPostage().add(o.getTotalPostage()));
            preOrderDto.setPayPrice(preOrderDto.getPayPrice().add(o.getPayPrice()));
            preOrderDto.setDeductionPrice(preOrderDto.getDeductionPrice().add(o.getDeductionPrice()));
            preOrderDto.setTotalNum(preOrderDto.getTotalNum() + o.getTotalNum());
        }

        //设置地址
        Long addressId = orderParam.getAddressId();
        YxUserAddress address=null;
        if(addressId==0){
        //    使用默认地址
            address = userAddressService.getDefaultAddress(orderParam.getUid());
        }else{
            //使用指定地址
            address = userAddressService.getById(orderParam.getAddressId());
        }
        preOrderDto.setAddress(address);

        // TODO: 2021/4/28 把preOrderDto放入缓存

        return preOrderDto;

    }



    // @ApiOperation(value = "提交订单")
    // @PostMapping(value = "/add")
    // public ResponseEntity addOrder() {
    //
    //
    //     return new ResponseEntity(HttpStatus.OK);
    // }


    @ApiOperation(value = "查询订单")
    @GetMapping(value = "/yxStoreOrder")
    public ResponseEntity getYxStoreOrders(YxStoreOrderQueryCriteria criteria,
                                           Pageable pageable,
                                           @RequestParam(name = "orderStatus") String orderStatus,
                                           @RequestParam(name = "orderType") String orderType) {


        criteria.setShippingType(1);//默认查询所有快递订单
        //订单状态查询
        if (StrUtil.isNotEmpty(orderStatus)) {
            switch (orderStatus) {
                case "1":
                    criteria.setIsDel(0);
                    criteria.setPaid(1);
                    criteria.setStatus(0);
                    criteria.setRefundStatus(0);
                    break;
                case "2":
                    criteria.setIsDel(0);
                    criteria.setPaid(1);
                    criteria.setStatus(1);
                    criteria.setRefundStatus(0);
                    break;
                case "3":
                    criteria.setIsDel(0);
                    criteria.setPaid(1);
                    criteria.setStatus(2);
                    criteria.setRefundStatus(0);
                    break;
                case "4":
                    criteria.setIsDel(0);
                    criteria.setPaid(1);
                    criteria.setStatus(3);
                    criteria.setRefundStatus(0);
                    break;
                case "-1":
                    criteria.setIsDel(0);
                    criteria.setPaid(1);
                    criteria.setRefundStatus(1);
                    break;
                case "-2":
                    criteria.setIsDel(0);
                    criteria.setPaid(1);
                    criteria.setRefundStatus(2);
                    break;
                case "-4":
                    criteria.setIsDel(1);
                    break;
            }
        }
        //订单类型查询
        if (StrUtil.isNotEmpty(orderType)) {
            switch (orderType) {
                case "2":
                    criteria.setNewCombinationId(0);
                    break;
                case "3":
                    criteria.setNewSeckillId(0);
                    break;
                case "4":
                    criteria.setNewBargainId(0);
                    break;
                case "5":
                    criteria.setShippingType(2);
                    break;
                default:
                    criteria.setBargainId(0);
                    criteria.setCombinationId(0);
                    criteria.setSeckillId(0);
            }
        }


        return new ResponseEntity(yxStoreOrderService.queryAll(criteria, pageable), HttpStatus.OK);
    }


}
